package cn.agiledata.bank.transaction.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.agiledata.bank.common.dao.AccountTemplateInter;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.DateUtil;
import cn.agiledata.bank.common.util.ForeDict;
import cn.agiledata.bank.common.util.PageBean;
import cn.agiledata.bank.common.util.SysbookUtil;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.hibernateBean.Sysbook;
import cn.agiledata.bank.hibernateBean.Transaction;
import cn.agiledata.bank.transaction.dao.RefundQueryPageList;
import cn.agiledata.bank.transaction.form.RefundQueryForm;
import cn.agiledata.bank.transaction.logic.RefundQueryBean;
import cn.agiledata.bank.transaction.logic.RefundTransaction;
import com.vnv.infrastructure.action.IPBBaseAction;
import com.vnv.infrastructure.logic.IPBAppFactory;

public class RefundTransAction extends IPBBaseAction {
    /**
     * 属于退款交易的交易状态列表
     */
    private static List REFUND_TRANS_STATUS_LIST;

    static {
        REFUND_TRANS_STATUS_LIST = new ArrayList();
        REFUND_TRANS_STATUS_LIST.add(Constant.TRANS_STATUS_APPLYREFUND); // 申请退款
        REFUND_TRANS_STATUS_LIST.add(Constant.TRANS_STATUS_DISPUTED); // 争议处理
        REFUND_TRANS_STATUS_LIST.add(Constant.TRANS_STATUS_REFUNDSUCC); // 退款成功
        REFUND_TRANS_STATUS_LIST.add(Constant.TRANS_STATUS_APPLYREFUNDCLOSE); // 取消退款
    }

    private AccountTemplateInter getAccountTemplate() {
        return (AccountTemplateInter) IPBAppFactory
                .getObj(ForeDict.ACCOUNT_TEMPLATE);
    }

    /**
     * 获取当前账户
     */
    private Account getCurrentAccount(HttpServletRequest request) {
        long accountId = WeatherUtil.getAccountIdFromSession(request);
        return getAccountTemplate().getAccount(String.valueOf(accountId));
    }

    /**
     * 
     * @return 退款交易的交易状态sysbook字典列表
     */
    private List getRefundTransStatusSysbooks() {
        List sysbooks = new ArrayList();
        Sysbook[] transStatuses = SysbookUtil.getTransStatuses();
        for (int i = 0; i < transStatuses.length; i++) {
            String listId = transStatuses[i].getId().getListId();
            if (REFUND_TRANS_STATUS_LIST.contains(listId)) {
                sysbooks.add(transStatuses[i]);
            }
        }
        return sysbooks;
    }

    /**
     * 退款交易查询
     */
    public final ActionForward refundQuery(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        Account account = getCurrentAccount(request);
        request.setAttribute("subAccounts", account.getSubAccounts());
        request.setAttribute("transStatusList", getRefundTransStatusSysbooks());
        RefundQueryForm form = (RefundQueryForm) actionForm;
        RefundQueryBean bean = new RefundQueryBean();
        bean.setAccount(account);
        if (form != null) {
            String subAccountId = form.getSubAccountId();
            if (subAccountId != null && subAccountId.length() > 0) {
                bean.setSubAccountId(new Long(subAccountId));
            }
            bean.setTransStatus(form.getTransStatus());
            String dateFormat = "yyyy-MM-dd";
            String beginDate = form.getBeginDate();
            if (beginDate != null && beginDate.length() > 0) {
                bean.setBeginDate(DateUtil.string2Date(beginDate, dateFormat));
            }
            String endDate = form.getEndDate();
            if (endDate != null && endDate.length() > 0) {
                bean.setEndDate(DateUtil.string2Date(endDate, dateFormat));
            }
            String peerAccount = form.getPeerAccount();
            if (peerAccount != null && peerAccount.length() > 0) {
                bean.setPeerAccount(getAccountTemplate().getAccountByName(
                        peerAccount));
            }
            bean.setTransId(form.getTransId());
            String minAmount = form.getMinAmount();
            if (minAmount != null && minAmount.length() > 0) {
                bean.setMinAmount(new Double(minAmount));
            }
            String maxAmount = form.getMaxAmount();
            if (maxAmount != null && maxAmount.length() > 0) {
                bean.setMaxAmount(new Double(maxAmount));
            }
            bean.setStartPage(form.getStartPage());
        }
        RefundQueryPageList pageList = new RefundQueryPageList(bean,
                REFUND_TRANS_STATUS_LIST);
        PageBean pb = pageList.getAccountListPageBean();
        List dataList = pb.getData();
        for (int i = 0; i < dataList.size(); i++) {
            Transaction trans = (Transaction) dataList.get(i);
            dataList.set(i, new RefundTransaction(trans));
        }
        request.setAttribute("result", pb);
        return mapping.findForward(FORWARD_SUCCESS);
    }
}
